<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//用户表社区系统消息表
class Bbs_notice_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->table = 'bbs_notice';
        $this->bbs_notice_status = 'bbs_notice_status';
    }
    //获取消息总数
    function get_notice_count($where){
        $this->db->select($this->table.'.*');
        $this->db->from($this->table);
        $this->db->where($where);
        return $this->db->get()->num_rows();
    }
    //获取消息列表
    function get_notice_list($where,$page,$limit){
        $this->db->select($this->table.'.*');
        $this->db->from($this->table);
        $this->db->where($where);
        $this->db->order_by($this->table.".create_time DESC");
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    
    //获取已读消息总数
    function get_notice_is_read_count($user_id){
        
        $sql = "select count(id) count from tan_bbs_notice_status where user_id =$user_id and notice_id  in(select id from tan_bbs_notice where recieve_id in($user_id,0) and is_show = 1)";
        $result = $this->db->query($sql)->row_array();
        return $result['count'];
    }
    //获取未读消息总数
    function get_notice_no_read_count($user_id){
        $sql = "select count(*) count from tan_bbs_notice where recieve_id in($user_id,0) and is_show = 1 and id not in(select notice_id from tan_bbs_notice_status where user_id = $user_id)";
        $result = $this->db->query($sql)->row_array();
        return $result['count'];
    }
    //获取未读消息列表
    function get_notice_no_read_list($user_id,$page,$limit){
        $sql = "select * from tan_bbs_notice where recieve_id in($user_id,0) and is_show = 1 and id not in(select notice_id from tan_bbs_notice_status where user_id = $user_id) ORDER BY create_time DESC limit $page,$limit";
        $result = $this->db->query($sql)->result_array();
        return $result;
    }
    //获取未读消息列表
    function get_notice_id_no_read_list($user_id,$page,$limit){
        $sql = "select id from tan_bbs_notice where recieve_id in($user_id,0) and is_show = 1 and id not in(select notice_id from tan_bbs_notice_status where user_id = $user_id) ORDER BY create_time DESC limit $page,$limit";
        $result = $this->db->query($sql)->result_array();
        return $result;
    }
    //添加事件
    function insert($data){
        $data['create_time'] = time();
        $this->db->insert($this->table,$data);
        return $this->db->affected_rows();
    }
    
    //批量添加聊天信息记录
    function insert_notice_status_batch($data){
        $this->db->insert_batch($this->bbs_notice_status, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
}
